﻿using MongoDB.Driver;
using Sustainalytics.Entities.RiskRatings;
using System.Collections.Generic;
using System.Linq;

namespace Sustainalytics.ScreeningTool.Synchronization.Handlers.RiskRatings
{
    public class CoreIndicatorHandler : IHandlerWithGrouping<CoreRiskRatingIndicator>
    {
        public ProjectionDefinition<CoreRiskRatingIndicator> GetProjection(ProjectionDefinitionBuilder<CoreRiskRatingIndicator> builder) => builder
            .Include(x => x.IndicatorType)
            .Include(x => x.Code)
            .Include(x => x.Name)
            .Include(x => x.Comment)
            .Include(x => x.AnswerCategory)
            .Include(x => x.AnswerCategoryText)
            .Include(x => x.RawScore)
            .Include(x => x.FinalWeight)
            .Include(x => x.WeightedScore);

        public IEnumerable<ExtraElement> GetExtraElements(IEnumerable<CoreRiskRatingIndicator> entities)
             => entities.SelectMany(entity =>
             {
                 var key = entity.GetIndicatorKey();
                 return new ExtraElementList
                 {
                     { $"{key}-Commentary-RR",                  entity.Comment             },
                     { $"{key}-Answer Category-RR",             entity.GetAnswerCategory() },
                     { $"{key}-Raw Score-RR",                   entity.RawScore            },
                     { $"{key}-Weight-Consolidated-RR",         entity.FinalWeight         },
                     { $"{key}-Weighted Score-Consolidated-RR", entity.WeightedScore       },
                 };
             });
    }
}
